local skel = fk.CreateSkill {
  name = "rmt_hongdang__xingbian",
  tags = { Skill.Permanent, Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["rmt_hongdang__xingbian"] = "星变",
  [":rmt_hongdang__xingbian"] = "持恒技，锁定技，若场上没有【闪电】，你的判定阶段改为回复1点体力；否则所有角色除〖星变〗以外的技能失效，"..
  "且于判定阶段结束后结束当前回合。",

  ["$rmt_hongdang__xingbian"] = "剥龙换骨星变易，富贵催人无歇灭。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return player.phase == Player.Judge and not data.phase_end and
        not table.find(player.room.alive_players, function (p)
          return p:hasDelayedTrick("lightning")
        end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:isWounded() then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name,
      })
    end
    data.phase_end = true
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return data.phase == Player.Judge and
        table.find(player.room.alive_players, function (p) return p:hasDelayedTrick("lightning") end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "rmt__xingbian_count", 1) -- 防止闪电不炸导致的死循环
    room:endTurn()
    room:delay(200)
  end,
})

skel:addEffect("filter", {
  card_filter = function(self, to_select, player, isJudgeEvent)
    return isJudgeEvent and table.find(Fk:currentRoom().alive_players, function (p)
      return p:getMark("rmt__xingbian_count") > 0 and p:getMark("rmt__xingbian_count") % 20 == 0
    end)
  end,
  view_as = function (self, player, card)
    return Fk:cloneCard(card.name, Card.Spade, 3)
  end,
})

skel:addEffect("invalidity", {
  invalidity_func = function(self, from, skill)
    return skill:isPlayerSkill(from) and not ((skill:getSkeleton() or {}).name == skel.name) and
    table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasSkill(skel.name)
    end) and
    table.find(Fk:currentRoom().alive_players, function (p)
      return p:hasDelayedTrick("lightning")
    end)
  end
})


return skel
